

// store/reducer.js
import {
  ADD_NUMBER,
  SUB_NUMBER,
  ADD_BANNER,
  ADD_RECOMMEND,
  FETCH_MULTI_DATA_FAIL
} from './constants.js'

// counterReducer
const inintStateCounter = {
  counter: 0,
}
function counterReducer(state = inintStateCounter, action){
  switch(action.type){
    case ADD_NUMBER:
      return {...state, counter: state.counter + action.num}
    case SUB_NUMBER:
      return {...state, counter: state.counter - action.num}
    default:
      return state
  }
}

// fetchDataReducer
const initStateFetchData = {
  banners: [],
  recommend: [],
  errMsg: ''
}
function fetchDataReducer(state = initStateFetchData, action){
  switch(action.type){
    case ADD_BANNER:
      return {...state, banners: [...state.banners, ...action.banners]}
    case ADD_RECOMMEND:
      return {...state, recommend: [...state.recommend, ...action.recommend]}
    case FETCH_MULTI_DATA_FAIL:
      return {...state, errMsg: action.errMsg}
    default:
      return state
  }
}

// state  前一个state
export default function reducer(state = {}, action){
  return {
    counterInfo: counterReducer(state.counterInfo, action),
    fetchDataInfo: fetchDataReducer(state.fetchDataInfo, action),
  }
}